/*     */ package com.zimbra.cs.mime;
/*     */ 
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import javax.mail.BodyPart;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.internet.MimeBodyPart;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.mail.internet.MimeMultipart;
/*     */ import javax.mail.internet.MimePart;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class MimeVisitor
/*     */ {
/*  43 */   private static List<Class<? extends MimeVisitor>> sMimeConverters = new ArrayList();
/*     */   
/*     */ 
/*  46 */   private static List<Class<? extends MimeVisitor>> sMimeMutators = new ArrayList();
/*     */   protected ModificationCallback mCallback;
/*     */   
/*     */   static {
/*  50 */     try { if (LC.zimbra_converter_enabled_uuencode.booleanValue())
/*  51 */         registerConverter(UUEncodeConverter.class);
/*     */     } catch (Exception e) {
/*  53 */       ZimbraLog.misc.error("error loading UUENCODE converter", e);
/*     */     }
/*     */     try
/*     */     {
/*  57 */       if (LC.zimbra_converter_enabled_tnef.booleanValue())
/*  58 */         registerConverter(TnefConverter.class);
/*     */     } catch (Exception e) {
/*  60 */       ZimbraLog.misc.error("error loading TNEF converter", e);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void registerConverter(Class<? extends MimeVisitor> vclass)
/*     */   {
/*  69 */     if (vclass != null) {
/*  70 */       ZimbraLog.lmtp.debug("Registering MIME converter: %s", new Object[] { vclass.getName() });
/*  71 */       sMimeConverters.add(vclass);
/*     */     }
/*     */   }
/*     */   
/*     */   public static void unregisterConverter(Class<? extends MimeVisitor> vclass)
/*     */   {
/*  77 */     if (vclass != null) {
/*  78 */       ZimbraLog.lmtp.debug("Unregistering MIME converter: %s", new Object[] { vclass.getName() });
/*  79 */       sMimeConverters.remove(vclass);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public static List<Class<? extends MimeVisitor>> getConverters()
/*     */   {
/*  86 */     return new ArrayList(sMimeConverters);
/*     */   }
/*     */   
/*     */ 
/*     */   public static boolean anyConvertersRegistered()
/*     */   {
/*  92 */     return !sMimeConverters.isEmpty();
/*     */   }
/*     */   
/*     */ 
/*     */   public static void registerMutator(Class<? extends MimeVisitor> vclass)
/*     */   {
/*  98 */     if (vclass != null) {
/*  99 */       ZimbraLog.lmtp.debug("Registering MIME mutator: %s", new Object[] { vclass.getName() });
/* 100 */       sMimeMutators.add(vclass);
/*     */     }
/*     */   }
/*     */   
/*     */   public static void unregisterMutator(Class<? extends MimeVisitor> vclass)
/*     */   {
/* 106 */     if (vclass != null) {
/* 107 */       ZimbraLog.lmtp.debug("Unregistering MIME mutator: %s", new Object[] { vclass.getName() });
/* 108 */       sMimeMutators.remove(vclass);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public static List<Class<? extends MimeVisitor>> getMutators()
/*     */   {
/* 115 */     return new ArrayList(sMimeMutators);
/*     */   }
/*     */   
/*     */ 
/*     */   public static boolean anyMutatorsRegistered()
/*     */   {
/* 121 */     return !sMimeMutators.isEmpty();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public MimeVisitor setCallback(ModificationCallback callback)
/*     */   {
/* 143 */     this.mCallback = callback;
/* 144 */     return this;
/*     */   }
/*     */   
/*     */   public ModificationCallback getCallback() {
/* 148 */     return this.mCallback;
/*     */   }
/*     */   
/*     */   protected static enum VisitPhase
/*     */   {
/* 153 */     VISIT_BEGIN,  VISIT_END;
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     private VisitPhase() {}
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public final synchronized boolean accept(MimeMessage mm)
/*     */     throws MessagingException
/*     */   {
/* 178 */     return accept(mm, 0);
/*     */   }
/*     */   
/* 181 */   private static final int MAX_VISITOR_DEPTH = LC.zimbra_converter_depth_max.intValue();
/*     */   
/*     */   private final synchronized boolean accept(MimePart mp, int depth) throws MessagingException
/*     */   {
/* 185 */     if (depth >= MAX_VISITOR_DEPTH) {
/* 186 */       return false;
/*     */     }
/* 188 */     boolean modified = false;
/*     */     
/* 190 */     if ((mp instanceof MimeMessage)) {
/* 191 */       modified |= visitMessage((MimeMessage)mp, VisitPhase.VISIT_BEGIN);
/*     */     }
/* 193 */     String ctype = Mime.getContentType(mp);
/* 194 */     boolean isMultipart = ctype.startsWith("multipart/");
/* 195 */     boolean isMessage = (!isMultipart) && (ctype.equals("message/rfc822"));
/*     */     
/* 197 */     if (isMultipart) {
/* 198 */       Object content = null;
/*     */       try {
/* 200 */         content = Mime.getMultipartContent(mp, ctype);
/*     */       } catch (Exception e) {
/* 202 */         ZimbraLog.extensions.warn("could not fetch multipart content; skipping", e);
/*     */       }
/* 204 */       if ((content instanceof MimeMultipart)) {
/* 205 */         MimeMultipart multi = (MimeMultipart)content;
/* 206 */         boolean multiModified = false;
/*     */         
/* 208 */         if (visitMultipart(multi, VisitPhase.VISIT_BEGIN)) {
/* 209 */           modified = multiModified = 1;
/*     */         }
/*     */         try {
/* 212 */           for (int i = 0; i < multi.getCount(); i++) {
/* 213 */             BodyPart bp = multi.getBodyPart(i);
/* 214 */             if ((bp instanceof MimeBodyPart)) {
/* 215 */               if (accept((MimeBodyPart)bp, depth + 1))
/* 216 */                 modified = multiModified = 1;
/*     */             } else {
/* 218 */               ZimbraLog.extensions.info("unexpected BodyPart subclass: " + bp.getClass().getName());
/*     */             }
/*     */           }
/*     */         } catch (MessagingException e) {
/* 222 */           ZimbraLog.extensions.warn("could not fetch body subpart; skipping remainder", e);
/*     */         }
/*     */         
/* 225 */         if (visitMultipart(multi, VisitPhase.VISIT_END))
/* 226 */           modified = multiModified = 1;
/* 227 */         if (multiModified)
/* 228 */           mp.setContent(multi);
/*     */       }
/* 230 */     } else if (isMessage) {
/* 231 */       MimeMessage content = null;
/*     */       try {
/* 233 */         content = Mime.getMessageContent(mp);
/*     */       } catch (Exception e) {
/* 235 */         ZimbraLog.extensions.warn("could not fetch attached message content; skipping", e);
/*     */       }
/* 237 */       if (content != null)
/* 238 */         modified |= accept(content, depth + 1);
/* 239 */     } else if ((mp instanceof MimeBodyPart)) {
/* 240 */       modified |= visitBodyPart((MimeBodyPart)mp);
/* 241 */     } else if (!(mp instanceof MimeMessage)) {
/* 242 */       ZimbraLog.extensions.info("unexpected MimePart subclass: " + mp.getClass().getName() + " (ctype='" + ctype + "')");
/*     */     }
/*     */     
/* 245 */     if ((mp instanceof MimeMessage)) {
/* 246 */       MimeMessage mm = (MimeMessage)mp;
/* 247 */       modified |= visitMessage(mm, VisitPhase.VISIT_END);
/*     */       
/*     */ 
/* 250 */       if (modified) {
/* 251 */         mm.saveChanges();
/*     */       }
/*     */     }
/* 254 */     return modified;
/*     */   }
/*     */   
/*     */   protected abstract boolean visitMessage(MimeMessage paramMimeMessage, VisitPhase paramVisitPhase)
/*     */     throws MessagingException;
/*     */   
/*     */   protected abstract boolean visitMultipart(MimeMultipart paramMimeMultipart, VisitPhase paramVisitPhase)
/*     */     throws MessagingException;
/*     */   
/*     */   protected abstract boolean visitBodyPart(MimeBodyPart paramMimeBodyPart)
/*     */     throws MessagingException;
/*     */   
/*     */   public static abstract interface ModificationCallback
/*     */   {
/*     */     public abstract boolean onModification();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mime/MimeVisitor.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */